﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace WMSSoft.Lib.WinKStart
{
    public class CDelegatingCommand : ICommand
    {
        /// <summary>
        /// delegate to run command
        /// </summary>
        private readonly Action<object> m_delegateExecute;
        /// <summary>
        /// Delegate for running command status
        /// </summary>
        private readonly Predicate<object> m_delegateCanExecute;

        private object m_DelegateParameter = null;

        public CDelegatingCommand(Action<object> Delegate)
        {
            m_delegateExecute = Delegate;
        }

        public object CommandParameter
        {
            get { return m_DelegateParameter; }
        }


        public CDelegatingCommand(Action<object> Delegate, Predicate<object> CanExecuteDelegate)
        {
            m_delegateExecute = Delegate;
            m_delegateCanExecute = CanExecuteDelegate;
        }


        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object Parameter)
        {
            return (m_delegateCanExecute == null) ? true : m_delegateCanExecute(Parameter);
        }

        public void Execute(object Parameter)
        {
            m_DelegateParameter = Parameter;
            m_delegateExecute(Parameter);
        }
    }
}
